package code.main.view.table
{
	import code.common.assets.AssetsClassName;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Circ;
	import com.yy.manager.ResourceManager;
	import com.yy.tools.arry.ArrayTools;
	import com.yy.tools.graphics.GT;
	import com.yy.tools.math.NumberTool;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import room.Chips1;
	import room.Chips10;
	import room.Chips100;
	import room.Chips1000;
	import room.Chips10000;
	import room.Chips100000;
	import room.Chips1000000;
	import room.Chips10000000;
	import room.Chips2;
	import room.Chips20;
	import room.Chips200;
	import room.Chips2000;
	import room.Chips20000;
	import room.Chips200000;
	import room.Chips2000000;
	import room.Chips20000000;
	import room.Chips5;
	import room.Chips50;
	import room.Chips500;
	import room.Chips5000;
	import room.Chips50000;
	import room.Chips500000;
	import room.Chips5000000;
	
	public class ChipsDisplay extends Sprite
	{
		//test
		private var _clsArr:Object = {
			Chips1:Chips1,Chips2:Chips2, Chips5:Chips5,
			Chips10:Chips10,Chips20:Chips20, Chips50:Chips50,
			Chips100:Chips100,Chips200:Chips200, Chips500:Chips500,
			Chips1000:Chips1000,Chips2000:Chips2000, Chips5000:Chips5000,
			Chips10000:Chips10000,Chips20000:Chips20000, Chips50000:Chips50000,
			Chips100000:Chips100000,Chips200000:Chips200000, Chips500000:Chips500000,
			Chips1000000:Chips1000000,Chips2000000:Chips2000000, Chips5000000:Chips5000000,
			Chips10000000:Chips10000000,Chips20000000:Chips20000000
		};
		private static const CHIP_THICK:Number = 2.5;//筹码y间隔
		private static const CHIP_DIAMETER:Number = 21;//筹码直径
		private static const MAX_COUNT:int = 5;//一个筹码住最多的筹码数量
		private static const  CHIPS_VALUE_LIST:Array = [
			1,2,5,
			10,20,50,
			100,200,500,
			1000,2000,5000,
			10000,20000,50000,
			100000,200000,500000,
			1000000,2000000,5000000,
			10000000,20000000//,50000000
		];
		private var _container:DisplayObjectContainer;
		private var _xPos:Number;
		private var _yPos:Number;
		private var _moveToUserCB:Function;//筹码移动到玩家完毕回调
		private var _moveToPotCB:Function;//筹码移动到奖池完毕回调
		private var _chipsConts:Array;//筹码容器
		private var _chipsArr:Array=[];//筹码数组容器
		private var _spaceTime:Number = .1;
		public var chipsBase:MovieClip;//筹码背景
		public function ChipsDisplay(prt:DisplayObjectContainer, xPos:Number=0, yPos:Number=0)
		{
			_container = prt;;
			_xPos = xPos;
			_yPos = yPos;
			_chipsConts = null;
			chipsBase = ResourceManager.getMovieClipByName(AssetsClassName.ChipsBase);
			addChild(chipsBase);
			chipsBase.visible = false;
			chipsBase.x = _xPos;
			chipsBase.y = _yPos+15;
			init();
		}
		private function init():void {
			_chipsConts = [new Sprite(), new Sprite(), new Sprite()];
			var i:int;
			var len:int = _chipsConts.length;
			for(i=0; i<len; i++) {
				addChild(_chipsConts[i]);
				_chipsConts[i].x = i*CHIP_DIAMETER;
				_chipsConts[i].y = 0;
			}
			_container.addChild(this);
		}
		/**
		 * 将数值转换成筹码数字
		 * 
		 */
		public function fromatToChipsArr(value:Number):Array {
			var arr:Array = [];
			var temp:int;
			var j:int = CHIPS_VALUE_LIST.length-1;
			var len:int = CHIPS_VALUE_LIST.length;
			var i:int;
			for(i=0; i<len; i++) {
				temp = value/CHIPS_VALUE_LIST[j];
				ArrayTools.addElsByCount(arr, CHIPS_VALUE_LIST[j],temp);
				value = value%CHIPS_VALUE_LIST[j];
				j--;
			}
			var arr2:Array = [[],[],[]];//拆分成3个筹码注的值
			j=0;
			while (arr.length>0) {
				temp = arr.length>MAX_COUNT?MAX_COUNT:arr.length;
				arr2[j++] = arr.splice(0,temp);
			}
			return arr2;
		}
		
		/**
		 *清除筹码 
		 * 
		 */
		public function clearChips():void {
			var i:int;
			var len:int = _chipsConts.length;;
			for(i=0; i<len; i++){
				GT.removeAllChild(_chipsConts[i]);
			}
			chipsBase.visible = false;
			chipsBase.label.text = 0;
			_chipsArr = [];
		}
		
		/**
		 *移动筹码到奖池 
		 * @param value
		 * @param sPos
		 * 
		 */
		public function moveChipsToPot(value:int, sPos:Array=null, callBack:Function=null):void{
			clearChips();
			_moveToPotCB = callBack;
			chipsBase.label.text = 0;
			chipsBase.visible = true;
			var arr:Array = fromatToChipsArr(value);
			var arr2:Array;
			var i:int;
			var j:int;
			var len:int = arr.length;
			trace("chips len:" + len);
			var len2:int;
			var c:DisplayObject;
			for(i=0; i<len; i++) {
				arr2 = arr[i];
				len2 = arr2.length;
				for(j=0; j<len2; j++) {
					c = new _clsArr["Chips" + arr2[j]];
					MovieClip(c).value = arr2[j];
					this.addChild(c);
					_chipsArr.push(c);
					if(sPos) {
						c.x = sPos[0];
						c.y = sPos[1];
						c.alpha = .5;
						TweenLite.to(
							c, 
							.5, 
							{
								x:_xPos,
								y:_yPos,
								alpha:1,
								delay:_spaceTime*j, 
								ease:Circ.easeOut,
								onComplete:moveChipsToPotCB,
								onCompleteParams:[c,_chipsConts[i], arr2[j]]
							}
						);
					}else{
						moveChipsToPotCB(c, _chipsConts[i], arr2[j]);
					}
				}
			}
			
			/**
			 *移动筹码到奖池回调 
			 * 
			 */
			function moveChipsToPotCB(c:DisplayObject, container:DisplayObjectContainer, value:Number):void{
				container.addChild(c);
				c.x = _xPos + NumberTool.getNumberInRange(-1, 1);
				c.y = _yPos + (-(CHIP_THICK) * container.numChildren);
				chipsBase.label.text = Number(chipsBase.label.text) + value;
				if(getChildNumb() == _chipsArr.length) {
					chipsBase.visible = true;
					if(Boolean(_moveToPotCB)) {
						chipsBase.label.text = value;
						_moveToPotCB();
					}
				}
			}
		}
		
		
		public function chipDispose():void
		{
			for(var i:int=0;i<this.numChildren;i++)
			{
				if (this.getChildAt(i) is DisplayObject )
				{
					this.removeChild(this.getChildAt(i) as DisplayObject);
				}	
			}
		}
		
		/**
		 * 移动奖池筹码给玩家
		 * @param seatId
		 * @param chips
		 * 
		 */
		public function movePotChipsToUser(callBack:Function, endPos:Array):void {
			_moveToUserCB = callBack;
			var i:int;
			_chipsArr.reverse();
			var len:int = _chipsArr.length;
			var pos:Array = endPos;
			var dobj:DisplayObject;
			for(i=0; i<len; i++) {
				dobj = _chipsArr[i];
				TweenLite.to(
					dobj, 
					.5, 
					{
						x:pos[0],
						y:pos[1],
						alpha:.5,
						onComplete:movePotChipsToUserCB,
						onCompleteParams:[dobj],
						delay:_spaceTime*i, 
						ease:Circ.easeOut
					}
				);
			}
			/**
			 *从奖池移动筹码到用户 
			 * @param dobj
			 * 
			 */
			function movePotChipsToUserCB(dobj:DisplayObject):void {
				trace("movePotChipsToUserCB");
				if(dobj.parent) {
					dobj.parent.removeChild(dobj);
					if(int(Object(dobj).value)>0) {
						chipsBase.label.text = Number(chipsBase.label.text) - int(Object(dobj).value); 
						if(int(chipsBase.label.text) < 0) {
							chipsBase.label.text = 0;
						}
					}
				}
				trace("if come to callBack"+getChildNumb());
				if(getChildNumb() < 1) {
					if(Boolean(_moveToUserCB)) {
						trace("come to callback");
						chipsBase.visible = false;
						clearChips();
						_moveToUserCB();
					}
				}
			}
		}
		
		/**
		 *活得筹码容器上的筹码个数 
		 * @return 
		 * 
		 */
		public function getChildNumb():int {
			var numb:int = 0;
			var i:int;
			var len:int = _chipsConts.length;
			var sp:Sprite;
			for(i=0; i<len; i++) {
				sp = _chipsConts[i];
				numb += sp.numChildren;
			}
			return numb;
		}
		
		/**
		 *从一个玩家移动筹码到另一个玩家 
		 * @param seatId
		 * @param toSeatId
		 * @param value
		 * 
		 */
		public function moveChipsToUser(seatId:int, toSeatId:int, value:Number):void {
			
		}
	}
}